package lendingbooks.activities;

import java.util.HashMap;

import lendingbooks.copy.Copy;
import lendingbooks.utils.ServiceCallException;
import lendingbooks.utils.ServiceManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class CopyActivity extends Activity {

	private Copy copy;

	private TextView isbn;
	private TextView title;
	private TextView authors;
	private RatingBar rattingBar;
	private Button removeButton;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.copy);
		copy = (Copy) getIntent().getExtras().getSerializable("copy");
		loadCopyInView(copy);
	}

	public void loadCopyInView(Copy p_copy) {
		this.isbn = (TextView) findViewById(R.id.copy_isbn);
		this.title = (TextView) findViewById(R.id.copy_title);
		this.authors = (TextView) findViewById(R.id.copy_authors);
		this.rattingBar = (RatingBar) findViewById(R.id.copy_rating);
		this.title.setText(p_copy.getTitle());
		this.authors.setText(p_copy.getAuthorsText());
		this.isbn.setText(p_copy.getIsbn());
		Integer rate = p_copy.getRate();
    	rate = rate==null?0:rate;
		this.rattingBar.setRating((float)rate);
		this.removeButton = (Button) findViewById(R.id.remove_copy_button);
		this.rattingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			public void onRatingChanged(RatingBar p_ratingBar, float p_rating, boolean p_fromUser) {
				copy.setRate((int) p_rating);
				_UpdateRating();
			}
		});

		this.removeButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				_DeleteCopy();
			}
		});
		// TODO removeButton listener
	}

	private void _UpdateRating() {
		final ProgressDialog dialog = ProgressDialog.show(CopyActivity.this, "", "Rating copy... Please wait...", true);
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				dialog.dismiss();
				_Back();
			}
		};

		Thread updateRating = new Thread() {
			@Override
			public void run() {
				HashMap<String, String> params = new HashMap<String, String>();
				params.put("copy_id", copy.getId().toString());
				params.put("rate", copy.getRate().toString());
				ServiceManager sm = new ServiceManager();
				try {
					sm.POST(ServiceManager.SERVICE.RATE_COPY, params);
					
				} catch (ServiceCallException e) {
					e.printStackTrace();
				}
				handler.sendEmptyMessage(0);
			}
		};
		updateRating.start();
	}

	private void _DeleteCopy() {
		final ProgressDialog dialog = ProgressDialog
				.show(CopyActivity.this, "", "Deleting copy... Please wait...", true);
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				dialog.dismiss();
				_Back();
			}
		};

		Thread deleteCopy = new Thread() {
			@Override
			public void run() {
				ServiceManager sm = new ServiceManager();
				try {
					sm.DELETE(ServiceManager.SERVICE.DELETE_COPY, copy.getId().toString());
					
				} catch (ServiceCallException e) {
					e.printStackTrace();
				}
				handler.sendEmptyMessage(0);
			}
		};
		deleteCopy.start();
	}
	

	private void _Back() {
		Log.d("  LENDINGBOOKS CopyActivity._Back ", "INIT");
		((LendingBooksApplication) getApplication()).setMyLibraryChanged(true);
		finish();
		Log.d("  LENDINGBOOKS CopyActivity._Back ", "END");
	}

}
